﻿namespace WLFramework.Validation
{
    using System;
    using System.Text;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Text.RegularExpressions;
    using System.Collections.Generic;

    public static class RegexValidation
    {
        static RegexValidation()
        {
            Regex.CacheSize = 100;
        }

        /// <summary>
        /// 中国电话
        /// </summary> 
        private readonly static Regex RegCHTel = new Regex(@"^\d{3,4}-?\d{6,8}$");

        // 中国手机
        private readonly static Regex RegCHMobile = new Regex(@"^[+].*|(1(3[0-9]|5[0-35-9]|8[0235-9])\d{8})$");
        // 邮箱
        private readonly static Regex RegEmail = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
        // URL
        private readonly static Regex RegURL = new Regex(@"^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&amp;%_\./-~-]*)?$");
        // IP
        private readonly static Regex RegIP = new Regex("^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])$");
        // QQ号
        private readonly static Regex RegQQ = new Regex("^[1-9][0-9]{4,}");
        // 邮编
        private readonly static Regex RegPostCode = new Regex(@"^[1-9]d{5}(?!d)");
        // 中文
        private readonly static Regex RegCHZN = new Regex(@"^([\u4E00-\uFA29]|[\uE7C7-\uE7F3])*$");
        // 正常字符
        private readonly static Regex RegNormalChar = new Regex(@"[\w\d_]+");
        // 英文
        private readonly static Regex RegENZN = new Regex("^[A-Za-z]+$");

        #region 中国电话
        /// <summary>
        /// 中国电话
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsCHTel(string inputData)
        {
            Match m = RegCHTel.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 中国手机
        /// <summary>
        /// 中国手机
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsCHMobile(string inputData)
        {
            Match m = RegCHMobile.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 邮箱
        /// <summary>
        /// 邮箱
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsEmail(string inputData)
        {
            Match m = RegEmail.Match(inputData);
            return m.Success;
        }
        #endregion

        #region URL
        /// <summary>
        /// URL
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsURL(string inputData)
        {
            Match m = RegURL.Match(inputData);
            return m.Success;
        }
        #endregion

        #region IP
        /// <summary>
        /// IP
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsIP(string inputData)
        {
            Match m = RegIP.Match(inputData);
            return m.Success;
        }
        #endregion

        #region QQ号
        /// <summary>
        /// QQ号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsQQ(string inputData)
        {
            Match m = RegQQ.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 邮编
        /// <summary>
        /// 邮编
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsPostCode(string inputData)
        {
            Match m = RegPostCode.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 中文
        /// <summary>
        /// 检测是否有中文字符
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsCHZN(string inputData)
        {
            Match m = RegCHZN.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 正常字符
        /// <summary>
        /// 正常字符
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNormalChar(string inputData)
        {
            Match m = RegNormalChar.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 英文
        /// <summary>
        /// 英文
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsENZN(string inputData)
        {
            Match m = RegENZN.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 自定义正则表达式
        /// <summary>
        /// 自定义正则表达式
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsCustom(string inputData, string RegexText)
        {
            Regex RegCustom = new Regex(RegexText);
            Match m = RegCustom.Match(inputData);
            return m.Success;
        }
        #endregion


        #region 其他

        /// <summary>
        /// 检查字符串最大长度，返回指定长度的串
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <param name="maxLength">最大长度</param>
        /// <returns></returns>			
        public static string SqlText(string input, int maxLength)
        {
            if (input != null && input != string.Empty)
            {
                input = input.Trim();
                if (input.Length > maxLength)//按最大长度截取字符串
                    input = input.Substring(0, maxLength);
            }

            return input;
        }

        /// <summary>
        /// 字符串编码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string HtmlEncode(string input)
        {
            return HttpUtility.HtmlEncode(input);
        }


        public static string SqlTextClear(string sqlText)
        {
            if (sqlText == null)
            {
                return null;
            }
            if (sqlText == "")
            {
                return "";
            }
            sqlText = sqlText.Replace(",", "");//去除,
            sqlText = sqlText.Replace("<", "");//去除<
            sqlText = sqlText.Replace(">", "");//去除>
            sqlText = sqlText.Replace("--", "");//去除--
            sqlText = sqlText.Replace("'", "");//去除'
            sqlText = sqlText.Replace("\"", "");//去除"
            sqlText = sqlText.Replace("=", "");//去除=
            sqlText = sqlText.Replace("%", "");//去除%
            sqlText = sqlText.Replace(" ", "");//去除空格
            return sqlText;
        }
        #endregion


        #region 身份证号
        //// <summary>
        /// 验证身份证是否有效
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static bool IsIDCard(string Id)
        {
            if (Id.Length == 18)
            {
                bool check = IsIDCard18(Id);
                return check;
            }
            else if (Id.Length == 15)
            {
                bool check = IsIDCard15(Id);
                return check;
            }
            else
            {
                return false;
            }
        }

        public static bool IsIDCard18(string Id)
        {
            long n = 0;
            if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16)
                || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
            {
                return false;//数字验证
            }
            string address =
                "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x" +
                "15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(Id.Remove(2)) == -1)
            {
                return false;//省份验证
            }
            string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证
            }
            string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
            string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
            char[] Ai = Id.Remove(17).ToCharArray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
            }
            int y = -1;
            Math.DivRem(sum, 11, out y);
            if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
            {
                return false;//校验码验证
            }
            return true;//符合GB11643-1999标准
        }

        public static bool IsIDCard15(string Id)
        {
            long n = 0;
            if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
            {
                return false;//数字验证
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x" +
                "14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(Id.Remove(2)) == -1)
            {
                return false;//省份验证
            }
            string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证
            }
            return true;//符合15位身份证标准
        }

        #endregion
    }
}
